<script setup lang="ts">
import { MobileRules, PasswordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { loginByPswAPI, loginByCodeAPI } from '@/services/login'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useCodeLogin } from '@/composable'

const mobile = ref('')
const password = ref('')
const show = ref(false)
const checked = ref(false)
const user = useUserStore()
const route = useRoute()
const router = useRouter()
// 点击提交验证表单
const checkForm = async () => {
  if (!checked.value) {
    return showToast('请勾选同意协议')
  }
  // 发送请求，登录：验证码登录 | 密码登录，根据isPSW
  const res = isPSW.value
    ? await loginByPswAPI(mobile.value, password.value)
    : await loginByCodeAPI(mobile.value, code.value)
  // 登录成功之后将数据存到user仓库中
  user.updateUserInfo(res.data)
  // 登录成功之后，如果当前路由上存在有returnURL参数，则需要跳转到这个url地址上，如果没有则跳转到个人中心页面，需要使用类型断言，指定url地址
  router.replace((route.query.returnURL as string) || 'user')
  showSuccessToast('登录成功！')
}

// 进行登录方式切换
const isPSW = ref(true) // 默认是密码登录
const code = ref('')
// 点击按钮，发送验证码，进行手机号码的单独验证
const { time, sendCode, form } = useCodeLogin(mobile, 'login')
// 记录当前的returnURL
const store = useUserStore()
const saveUrl = () => {
  store.updateReturnURL(route.query.returnURL as string)
}
const url = encodeURIComponent(import.meta.env.VITE_APP_CALLBACK)
</script>

<template>
  <div class="login-page">
    <DP-NavBar
      rightText="注册"
      @clickRight="$router.push('/register')"
    ></DP-NavBar>
    <div class="login-header">
      <h3>{{ isPSW ? '密码登录' : '验证码登录' }}</h3>
      <a href="javascript:;" @click="isPSW = !isPSW">
        <span>{{ isPSW ? '验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form表单 -->
    <van-form class="dp-form" @submit="checkForm" ref="form">
      <van-field
        v-model.trim="mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="MobileRules"
        autocomplete="off"
      ></van-field>
      <!-- 判断短信验证码登录方式还是密码登录方式 -->
      <van-field
        v-if="isPSW"
        v-model="password"
        placeholder="请输入密码"
        :type="show ? 'text' : 'password'"
        :rules="PasswordRules"
        autocomplete="off"
      >
        <template #button>
          <DP-Icon
            @click="show = !show"
            :name="`login-eye-${show ? 'on' : 'off'}`"
          ></DP-Icon>
        </template>
      </van-field>
      <van-field
        v-else
        v-model="code"
        placeholder="请输入验证码"
        :rules="codeRules"
      >
        <template #button>
          <span
            :class="['sendCode', time > 0 ? 'active' : '']"
            @click="sendCode"
            >{{ time <= 0 ? '发送验证码' : `${time}s后重新发送` }}</span
          >
        </template>
      </van-field>
      <div class="dp-cell">
        <van-checkbox v-model="checked">
          <span>我已同意</span>
          <a href="javascript:;" style="color: var(--cp-primary); margin: 0 3px"
            >用户协议</a
          >
          <span>及</span>
          <a href="javascript:;" style="color: var(--cp-primary); margin: 0 3px"
            >隐私条款</a
          >
        </van-checkbox>
      </div>
      <div class="dp-cell">
        <van-button type="primary" block round native-type="submit"
          >登录</van-button
        >
      </div>
      <div class="dp-cell">
        <a href="javascript:;">忘记密码?</a>
      </div>
    </van-form>
    <div class="login-footer">
      <van-divider> 第三方登录 </van-divider>
      <a
        @click="saveUrl"
        class="icon"
        :href="`https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${url}`"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
.login-page {
  margin-top: 46px;
  .login-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 30px 30px 50px;
    h3 {
      font-size: 24px;
      font-weight: 400;
    }
    a {
      font-size: 15px;
      color: var(--cp-text2);
    }
  }
  .dp-form {
    padding: 0 14px;
    .dp-cell {
      display: flex;
      align-items: center;
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
    }
    .sendCode {
      color: var(--cp-primary);
      &.active {
        color: #16c2a380;
      }
    }
  }
  .login-footer {
    margin-top: 60px;
    padding: 0 30px;
    font-size: 12px;
    .icon {
      display: flex;
      justify-content: center;
      margin: 16px auto;
      img {
        width: 44px;
        height: 44px;
      }
    }
  }
}
</style>
